import 'package:file_picker/file_picker.dart';
import 'package:flutter/material.dart';
import 'package:get/get.dart';
import 'package:smart_devices/bruno/src/components/tabbar/normal/brn_tab_bar.dart';
import 'package:smart_devices/model/repair_order.dart';
import 'package:smart_devices/model/spare_part.dart';

class RepairDetailsLogic extends GetxController with GetTickerProviderStateMixin {
  RxList<BadgeTab> tabs = RxList([]);

  Rx<SparePartModel> details = Rx(
      SparePartModel(imgUrl: '', name: '', code: '', model: '', count: 0, change: 0, inventory: 0));

  Rx<RepairOrderModel> order = Rx(RepairOrderModel(
      title: '',
      orderNo: '',
      name: '',
      code: '',
      location: '',
      degree: RepairOrderDegreeEnum.fast,
      addTime: '',
      status: RepairOrderEnum.fast));

  late TabController tabController;

  RxList<String> imgs = RxList([
    'https://lmg.jj20.com/up/allimg/4k/s/02/210924233115O14-0-lp.jpg',
    'https://lmg.jj20.com/up/allimg/1114/0G020114924/200G0114924-15-1200.jpg',
    'https://lmg.jj20.com/up/allimg/1114/0406210Z024/2104060Z024-5-1200.jpg',
    'https://lmg.jj20.com/up/allimg/1114/0406210Z024/2104060Z024-5-1200.jpg',
    'https://lmg.jj20.com/up/allimg/1111/04131Q42551/1P413142551-4-1200.jpg',
    'https://lmg.jj20.com/up/allimg/tp08/24041224115258-lp.jpg'
  ]);

  RxList<PlatformFile> files = RxList([]);

  RxList<String> users = RxList(['楚楚', '香蕉', '子瑞', '林奇', '张柳']);
  RxInt userIndex = RxInt(0);

  RxList<GlobalKey> tabKeys = RxList([]);

  RxBool isTabClicked = RxBool(false);

  late ScrollController scrollController;

  @override
  void onInit() {
    super.onInit();
    files.add(PlatformFile(name: '文档.pdf', size: 25678));
    files.add(PlatformFile(name: '表格文档.xlsx', size: 25678));
    files.add(PlatformFile(name: '幻灯片文档.pptx', size: 25678));
    files.add(PlatformFile(name: '图片.png', size: 25678));
    files.add(PlatformFile(name: 'Word文档.docx', size: 25678));
    files.add(PlatformFile(name: '压缩文档.zip', size: 25678));
    tabs.add(BadgeTab(text: '故障信息'));
    tabs.add(BadgeTab(text: '审核信息'));
    tabs.add(BadgeTab(text: '处理进度'));
    tabs.add(BadgeTab(text: '维修处理'));
    tabs.add(BadgeTab(text: '工作量'));
    tabs.add(BadgeTab(text: '更换备件'));
    for (var i = 0; i < users.length; i++) {
      tabKeys.add(GlobalKey(debugLabel: 'tab_$i'));
    }
    scrollController = ScrollController()..addListener(onScroll);
    tabController = TabController(length: tabs.length, vsync: this);
    if (Get.arguments != null) {
      details.value = Get.arguments['details'] as SparePartModel;
      order.value = Get.arguments['order'] as RepairOrderModel;
    }
  }

  void onTabChange(i) {
    final keyRenderObject = tabKeys[i].currentContext?.findAncestorRenderObjectOfType();
    if (keyRenderObject != null) {
      // 点击的时候不让滚动影响tab
      isTabClicked.value = true;
      scrollController.position
          .ensureVisible(keyRenderObject,
              duration: const Duration(milliseconds: 300), curve: Curves.easeIn)
          .then((value) {
        isTabClicked.value = false;
      });
    }
  }

  void onScroll() {
    Get.log('offset = ${scrollController.offset}');
    if (isTabClicked.value) return;
    double height = 0.0;
    for (int i = 0; i < tabKeys.length; i++) {
      if (tabKeys[i].currentContext != null) {
        double offsetY = (tabKeys[i].currentContext!.findRenderObject() as RenderBox)
            .localToGlobal(Offset.zero)
            .dy;
        double h = tabKeys[i].currentContext?.size?.height ?? 0;
        Get.log('offsetY = $offsetY \th = $h');
        height = height + h;
        if (height > scrollController.offset) {
          userIndex.value = i;
          return;
        }
      }
    }
    userIndex.value = tabKeys.length - 1;
  }

  @override
  void onClose() {
    super.onClose();
    scrollController.removeListener(onScroll);
    scrollController.dispose();
    tabController.dispose();
  }
}
